package com.homework.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.homework.bin.user;
import com.homework.dao.userdao;
import com.homework.util.DBUtil1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("*.user")
public class userServlet extends HttpServlet {
	public userServlet() {
        super();
    }
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("GBK");
		String url=request.getServletPath();
		if(url.equals("/register.user")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			user ui = new user();
			ui.setUsername(username);
			ResultSet rs = null;
			try {
				rs = userdao.selectbyusername(ui);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (rs.next()) {
					request.getRequestDispatcher("/userExist.html").forward(request, response);
				} else {
					ui.setPassword(password);
					int i = userdao.insert(ui);
					if (i == 1)
						request.getRequestDispatcher("/successregister.html").forward(request, response);
					else
						request.getRequestDispatcher("/failregister.html").forward(request, response);
				}
			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		/*}else if(url.equals("/checkName.user")){
			String username=request.getParameter("username");
			UserInfo ui=new UserInfo();
			ui.setUsername(username);
			boolean exist=ui.checkName();
			if(exist)
				request.getRequestDispatcher("/userExist.html").forward(request, response);
			else
				request.getRequestDispatcher("/userNoExist.html").forward(request, response);*/
		}else if(url.equals("/login.user")){
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			user ui = new user();
			ui.setUsername(username);
			ResultSet rs = null;
try{
	ui.setPassword(password);
	rs=userdao.selectbyusernameanpassword(ui);
} catch (Exception e) {
	e.printStackTrace();
}
try{
	if(rs.next()){
		request.getRequestDispatcher("/successlogin.jsp").forward(request, response);
	}else {
		request.getRequestDispatcher("/faillogin.html").forward(request, response);
	}
} catch (Exception e) {
	e.printStackTrace();
}
		} else if(url.equals("/update.user")){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		user ui = new user();int i=0;
		ui.setUsername(username);
		ui.setPassword(password);
		try{
			i=userdao.update(ui);
		} catch (Exception e) {
			e.printStackTrace();
		}if(i>0) {
				request.getRequestDispatcher("/successupdate.html").forward(request, response);
			}else {
				request.getRequestDispatcher("/failupdate.html").forward(request, response);
			}
		}else if(url.equals("/delete.user")){
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			user ui = new user();int i=0;
			ui.setUsername(username);
			ui.setPassword(password);
			try{
				i=userdao.delete(ui);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i>0){
				request.getRequestDispatcher("/successdelete.html").forward(request, response);
			}else{
				request.getRequestDispatcher("/faildelete.html").forward(request, response);
			}
		}
	}
}
